﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Editions;
using Abp.Application.Features;
using Abp.Authorization.Users;
using Abp.Configuration.Startup;
using Abp.Dependency;
using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Abp.Domain.Uow;
using Abp.Events.Bus.Entities;
using Abp.Events.Bus.Handlers;
using Abp.IdentityFramework;
using Abp.Localization;
using Abp.MultiTenancy;
using Abp.Runtime.Caching;
using Abp.Runtime.Session;
using Microsoft.AspNet.Identity;
using XHD.WXZJ.Posts;

namespace XHD.WXZJ.Departments
{
    /// <summary>
    /// 部门管理类实现
    /// </summary>
    /// <typeparam name="TTenant"></typeparam>
    /// <typeparam name="TDepartment"></typeparam>
    /// <typeparam name="TUser"></typeparam>
    public class AbpDepartmentManager<TTenant,TDepartment,TUser> : IDomainService
      
        where TTenant : AbpTenant<TTenant, TUser>
        where TDepartment : AbpDepartment<TTenant, TUser>
        where TUser : AbpUser<TTenant, TUser>
    {
       public ILocalizationManager LocalizationManager { get; set; }
       protected IRepository<TDepartment> DepartmentRepository { get; set; }

      

       /// <summary>
       /// 构造函数
       /// </summary>
       /// <param name="departmentRepository"></param>
       public AbpDepartmentManager(IRepository<TDepartment> departmentRepository)
        {
            DepartmentRepository = departmentRepository;
        }
      
       private string L(string name)
       {
           return LocalizationManager.GetString(WXZJConsts.LocalizationSourceName, name);
       }
        /// <summary>
        /// 部门对象校验
        /// </summary>
        /// <param name="department"></param>
        /// <returns></returns>
       protected virtual async Task<IdentityResult> ValidateDepartmentAsync(TDepartment department)
       {
           var nameValidationResult = await ValidateDepartmentNameAsync(department.Name);
           return !nameValidationResult.Succeeded ? nameValidationResult : IdentityResult.Success;
       }

       /// <summary>
       /// 名称校验
       /// </summary>
       /// <param name="departmentName"></param>
       /// <returns></returns>
       protected virtual async Task<IdentityResult> ValidateDepartmentNameAsync(string departmentName)
       {
           return !Regex.IsMatch(departmentName, AbpDepartment<TTenant, TUser>.DepartmentNameRegex) ? AbpIdentityResult.Failed(L("InvalidDepartmentName")) : IdentityResult.Success;
       }

        /// <summary>
       /// 获取所有对象
       /// </summary>
       public virtual IQueryable<TDepartment> Departments
       {
           get { return DepartmentRepository.GetAll(); }
       }

       /// <summary>
       /// 新增一个部门对象
       /// </summary>
       /// <param name="department"></param>
       /// <returns></returns>
       public virtual async Task<IdentityResult> CreateAsync(TDepartment department)
       {
           if (await DepartmentRepository.FirstOrDefaultAsync(d => d.Name == department.Name) != null)
           {
               return AbpIdentityResult.Failed(string.Format(L("DepartmentNameIsAlreadyTaken")));
           }

           var validationResult = await ValidateDepartmentAsync(department);
           if (!validationResult.Succeeded)
           {
               return validationResult;
           }
           await DepartmentRepository.InsertAsync(department);

           return IdentityResult.Success;
       }

       /// <summary>
       /// 更新对象
       /// </summary>
       /// <param name="department"></param>
       /// <returns></returns>
       public virtual async Task<IdentityResult> UpdateAsync(TDepartment department)
       {
           if (await DepartmentRepository.FirstOrDefaultAsync(t => t.Name == department.Name && t.Id != department.Id) != null)
           {
               return AbpIdentityResult.Failed(string.Format(L("DepartmentNameIsAlreadyTaken"), department.Name));
           }

           await DepartmentRepository.UpdateAsync(department);
           return IdentityResult.Success;
          
       }

       /// <summary>
       /// 删除对象
       /// </summary>
       /// <param name="department"></param>
       /// <returns></returns>
       public virtual async Task<IdentityResult> DeleteAsync(TDepartment department)
       {
           
           await DepartmentRepository.DeleteAsync(department);
           return IdentityResult.Success;
       }

       /// <summary>
       /// 根据部门ID查找制定对象
       /// </summary>
       /// <param name="departmentId">部门ID</param>
       /// <returns></returns>
       public virtual async Task<TDepartment> GetByIdAsync(int departmentId)
       {
           var department = await DepartmentRepository.FirstOrDefaultAsync(departmentId);
           if (department == null)
           {
               throw new AbpException(string.Format("不存在部门id为{0}的部门", departmentId));
           }
           return department;
       }

       public virtual async Task<TDepartment> FindByIdAsync(int id)
       {
           return await DepartmentRepository.FirstOrDefaultAsync(id);
       }
       /// <summary>
       /// 根据部门名称查找部门对象
       /// </summary>
       /// <param name="departmentName">部门名称</param>
       /// <returns></returns>
       public virtual async Task<TDepartment> FindByNameAsync(string departmentName)
       {
           return await DepartmentRepository.FirstOrDefaultAsync(
               department => department.Name == departmentName
               );
       }

       /// <summary>
       /// 根据显示名称查找部门对象
       /// </summary>
       /// <param name="displayName">显示名称</param>
       /// <returns></returns>
       public virtual async Task<TDepartment> FindByDisplayNameAsync(string displayName)
       {
           return await DepartmentRepository.FirstOrDefaultAsync(
               department => department.DisplayName == displayName
               );
       }

       public virtual void Dispose()
       {
           //如果使用IOC则要在这里做释放
       }
      
    }
}
